from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.events import BeforeRender
from pyramid_beaker import session_factory_from_settings

from .security import rolefinder

from .models.meta import DBSession

from . import helpers

def add_renderer_globals(event):
    event['h'] = helpers

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    
    #session
    session_factory = session_factory_from_settings(settings)

    #add auth
    authn_policy = AuthTktAuthenticationPolicy('sosecret', callback=rolefinder)
    authz_policy = ACLAuthorizationPolicy()
    config = Configurator(
            settings=settings, 
            session_factory = session_factory,
            root_factory='.security.RootFactory'
            )
    config.add_subscriber(add_renderer_globals, BeforeRender)
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)
    
    #set the mako template related .html
    config.add_renderer('.html', 'pyramid.mako_templating.renderer_factory')

    #view config
    config.add_static_view('static', 'static', cache_max_age=3600)

    #ueditor
    config.add_route('ueditor_php', '/ueditor/php/{path}.php')

    #ajax validation
    config.add_route('validation1', '/{path}/ajaxValidateFieldUser')
    config.add_route('validation2', '/{path1}/{path2}/ajaxValidateFieldUser')
    config.add_route('validationSubmit', '/ajaxValidateSubmit')

    #captcha
    config.add_route('captcha', '/captcha')

    #------------------------------front--------------------------------------
    #user
    config.add_route('user_login', '/user/login.html')
    config.add_route('user_register', '/user/register.html')
    config.add_route('user_page', '/user/{page}.html')
    #business
    config.add_route('business_login', '/business/login.html')
    config.add_route('business_page', '/business/{page}.html')
    config.add_route('business_edit_page', '/business/{page}/{id}.html')
    #coupon 
    config.add_route('coupons_page1', '/coupons/{page}.html')
    config.add_route('coupons_page2', '/coupons/{page}/{id}.html')
    config.add_route('coupons_page3', '/coupons/auth/{page}.html')
    config.add_route('coupons_page4', '/coupons/auth/{page}/{id}.html')
    #product
    config.add_route('products_page1', '/products/category{category}/{page}.html')
    config.add_route('products_page2', '/products/{page}.html')
    config.add_route('products_page3', '/products/{page}/{id}.html')


    config.add_route('index1', '/index.html')
    config.add_route('index', '/')

    #------------------------------admin--------------------------------------
    #product order
    config.add_route('admin_productorder_list', '/admin/productorder/list.html')
    config.add_route('admin_productorder_edit', '/admin/productorder/edit.html')
    #product
    config.add_route('admin_productcategory_list', '/admin/productcategory/list.html')
    config.add_route('admin_productcategory_edit', '/admin/productcategory/edit.html')
    config.add_route('admin_productcategory_delete', '/admin/productcategory/delete.html')
    config.add_route('admin_product_list', '/admin/product/list.html')
    config.add_route('admin_product_edit', '/admin/product/edit.html')
    config.add_route('admin_product_delete', '/admin/product/delete.html')
    #coupons
    config.add_route('admin_coupon_list', '/admin/coupon/list.html')
    config.add_route('admin_coupon_edit', '/admin/coupon/edit.html')
    config.add_route('admin_coupon_delete', '/admin/coupon/delete.html')
    #user
    config.add_route('admin_user_list', '/admin/user/list.html')
    config.add_route('admin_user_edit', '/admin/user/edit.html')
    config.add_route('admin_user_delete', '/admin/user/delete.html')
    #business user
    config.add_route('admin_businessuser_list', '/admin/businessuser/list.html')
    config.add_route('admin_businessuser_edit', '/admin/businessuser/edit.html')
    config.add_route('admin_businessuser_delete', '/admin/businessuser/delete.html')

    config.add_route('admin_module_menu', '/admin/{module}/menu.html')


    #system
    config.add_route('admin_login', '/admin/login.html')
    config.add_route('admin_top', '/admin/top.html')
    config.add_route('admin_page', '/admin/{page}.html')
    config.add_route('admin_index', '/admin/')

    config.scan('coupons.views')
    return config.make_wsgi_app()

